function specialDetailCtrl($rootScope, $scope, $http, ngDialogProblem, $compile) {
  'ngInject';       
  $http
    .get(`${SERVER_CONTEXT}/api/checkRecord/app/queryContentList/${$scope.id}`,{})
    .then((resp) => {
      this.dailyCheckDetailList = resp.data;
      this.initMap(this.dailyCheckDetailList);
  });

  // 默认展示地图
  this.shouldMapShow = true;

  // 展示问题记录
  this.showProblemDetail = (id, index) => {
    this.shouldMapShow = false;
    this.selectedIndex = index;
    let _this = this;
    this.isUploading = false;

    this.reasons = [{
      id: 1,
      name: '管理制度',
      active: false
    }, {
      id: 2,
      name: '岗位职责',
      active: false
    }, {
      id: 3,
      name: '专项方案',
      active: false
    }, {
      id: 3,
      name: '安全措施',
      active: false
    }, {
      id: 4,
      name: '思想行为',
      active: false
    }, {
      id: 5,
      name: '经费保障',
      active: false
    }, {
      id: 6,
      name: '其他',
      active: false
    }];

    this.rectificationList = {};
    this.detailList = {
      problemModifyBean: {
        fileBeanList: [],
      },
    };
    // 获取专项检查详情-问题记录
    this.getSpecialDetail = () => {
      $http.get(`${SERVER_CONTEXT}/api/checkRecord/app/queryContentDetail/${id}`)
        .then((resp) => {
          this.detailList = resp.data;
          this.rectificationList = this.detailList.problemModifyBean ? this.detailList.problemModifyBean : {};
          this.approval = this.detailList.problemModifyBean ? this.detailList.problemModifyBean : {};
          handleProblemReasons2ViewModel(resp.data);

          function handleProblemReasons2ViewModel(respData) {
            if (respData && respData.problemModifyBean && respData.problemModifyBean.causeAnalysis) {
              let activeReasons = respData.problemModifyBean.causeAnalysis.split(',');
              activeReasons.forEach((activeReason) => {
                _this.reasons.forEach((reason) => {
                  if (reason.name == activeReason) {
                    reason.active = true;
                  }
                })
              });
            }
          }
        });
    }

    // 上传新增整改后照片
    this.submit = () => {
      var causeAnalysis = "";
      // 上传图片
      let fileBeanList = (this.detailList.problemModifyBean && this.detailList.problemModifyBean.fileBeanList) || [];
      // modifyImage 属性用于接收附件token 多个附件以逗号分隔

      this.rectificationList.problemImageIds = fileBeanList.map(item => item.id).join(',');
      this.rectificationList.problemId = id;

      // 原因分析
      this.reasons.forEach(function(item, index) {
        if (item.active) {
          causeAnalysis = causeAnalysis ? causeAnalysis + ',' + item.name : item.name;
        }
      });

      this.rectificationList.causeAnalysis = causeAnalysis;

      $http.post(`${SERVER_CONTEXT}/api/checkRecord/app/addProblemModify`,
          this.rectificationList
        )
        .then((resp) => {
          if (resp.data.success) {
            layer.msg(resp.data.message);
            this.getSpecialDetail();
            deferred.resolve(resp);
          } else {
            layer.alert(resp.data.message);
            deferred.reject(resp);
          }
        });
    }

    // 展示审批
    this.isAuditable = function() {
      if (this.detailList.problemFoundUserId == $rootScope.userInfo.userId && this.detailList.problemStatus == 1) {
        return true;
      }
      return false;
    }

    // 提交审批意见
    this.approvalSubmit = () => {
      let reqData = {
        checkResult: parseInt(this.approval.checkResult),
        problemId: id,
        comments: this.approval.comments
      }

      $http.post(`${SERVER_CONTEXT}/api/checkRecord/app/auditModifyProblem`,
          reqData
        )
        .then((resp) => {
          if (resp.data.success) {
            layer.msg(resp.data.message);
            // $state.reload();
            this.getSpecialDetail();
            deferred.resolve(resp);
          } else {
            layer.alert(resp.data.message);
            deferred.reject(resp);
          }
        });
    }

    // 文件预览
    this.preview = (archiveDetail) => {
      let url = $state.href('documentPreview', {
        id: archiveDetail.id,
      });
      window.open(url,'_blank');
    }

    // 重置审批意见
    this.reset = () => {
      this.approval = {};
    }

    this.initPage = () => {
      // 获取问题详情
      this.getSpecialDetail();
    }

    // 初始化页面
    this.initPage();
  }

  // 展示地图
  this.showMap = () => {
    this.shouldMapShow = true;
  }

  this.showProblemById = (problemId) => {
    console.log(problemId);
    console.log(this.dailyCheckDetailList);
    this.dailyCheckDetailList.forEach((problem, index) => {
      if (problem.problemId == problemId) {
        this.showProblemDetail(problemId, index);
      }
    });

  }

  /* 
   * immediate execution will throw error, this function should run after link function
   * running in ajax callback function is seen as a tricky way solving executive order problem
   */
  this.initMap = (problemsData) => {
    let _this = this;
  	let rawTrackData = $scope.trackData;
    let startPointImg = require(`../../../assets/images/position-start.png`);
    let problemPointImg = require(`../../../assets/images/position-ing.png`);
    let endPointImg = require(`../../../assets/images/position-end.png`);
    let imageData = {
    	startPointImg,
    	endPointImg,
    	problemPointImg,
    }

    let map = window.map = new BMap.Map("bdmap_container");
    let resPoints = createBdPoint(rawTrackData),
    		markerMap = createBdMarker(rawTrackData);

    map.enableScrollWheelZoom();
    drawTrackLines(map, resPoints);
    preAddMarkers(markerMap);
    addMarkers(map, markerMap);
    addMarkerEventHandlers(map, markerMap);

    function createBdPoint(rawData) {
    	let startPoi = new BMap.Point(rawData.startPoint.longitude, rawData.startPoint.latitude),
    	    endPoi   = new BMap.Point(rawData.endPoint.longitude, rawData.endPoint.latitude),
    	    problemPois = [],
    	    trackHistoryPoints = [];
    	rawData.problemPoints.forEach((poi) => {
    	  problemPois.push(new BMap.Point(poi[0], poi[1]));
    	});

    	rawData.trackHistoryPoints.forEach((poi) => {
    		trackHistoryPoints.push(new BMap.Point(poi[0], poi[1]));
    	});

    	let resData = {
    		startPoint: {
    			poi: startPoi,
    			time: rawData.startPoint.createTime1,
    		},

    		endPoint: {
    			poi: endPoi,
    			time: rawData.endPoint.createTime1,
    		},

    		problemPoints: problemPois,

    		trackHistoryPoints: trackHistoryPoints,
    	}
    	return resData;
    }

    function createBdMarker(rawData) {
      let startPoi = new BMap.Point(rawData.startPoint.longitude, rawData.startPoint.latitude),
          endPoi   = new BMap.Point(rawData.endPoint.longitude, rawData.endPoint.latitude);

      let startMarker = new BMap.Marker(startPoi, {icon:new BMap.Icon(imageData.startPointImg, new BMap.Size(36, 36))});
      startMarker.time = rawData.startPoint.createTime1;

      let endMarker = new BMap.Marker(endPoi, {icon:new BMap.Icon(imageData.endPointImg, new BMap.Size(36, 36))});
      endMarker.time = rawData.endPoint.createTime1;

      let problemMarkers = [];
      let problemIcon = new BMap.Icon(imageData.problemPointImg, new BMap.Size(36, 36));
      rawData.problemPoints.forEach((problemPoint) => {
        let bdPoi = new BMap.Point(problemPoint[0], problemPoint[1]) 
        let problemMarker = new BMap.Marker(bdPoi, {icon: problemIcon});
        problemMarker.time = problemPoint[2];
        problemMarker.problemId = problemPoint[3];
        problemsData.forEach((problemData) => {
          if (problemData.problemId == problemPoint[3]) {
            problemMarker.problemContent = problemData.contentNumber;
          }
        });
        problemMarkers.push(problemMarker);
      });
      let resMarkerMap = {
        startMarker,
        endMarker,
        problemMarkers,
      }
      return resMarkerMap;
    }

    function preAddMarkers(markerMap) {
      let markers = [markerMap.startMarker, markerMap.endMarker, ...markerMap.problemMarkers];

      const ERROR_RANGE = 0.00007;

      bubbleSort(markers);
      mutatedBubbleCompare(markers);
      // compare location of map markers, if two points differs less than ERROR_RANGE value, mutate point property of marker.
      function mutatedBubbleCompare(markers) {
        let l = markers.length, i = 0;
        while (i < l - 1) {
          for (let j = i; j < l - 1; j++) {
            let preMarker = markers[j],
                currentMarker = markers[j + 1];
            let prePoint = markers[j].point;
            let currentPoint = markers[j + 1].point;
            if (Math.abs(prePoint.lng - currentPoint.lng) < ERROR_RANGE || Math.abs(prePoint.lat - currentPoint.lat) < ERROR_RANGE) {
              let point = new BMap.Point(prePoint.lng + ERROR_RANGE/2, prePoint.lat + ERROR_RANGE/2);
              preMarker.setPosition(point);
            }
          }
          i++;
        }
      }

     function bubbleSort(markers) {
      let i = markers.length, j;
      let tempExchangVal;
      while (i > 0) {
       for (j = 0; j < i - 1; j++) {
        if (markers[j].point.lng > markers[j + 1].point.lng) {
         tempExchangVal = markers[j];
         markers[j] = markers[j + 1];
         markers[j + 1] = tempExchangVal;
        } else if (markers[j].point.lng == markers[j + 1].point.lng && markers[j].point.lat > markers[j + 1].point.lat) {
         tempExchangVal = markers[j];
         markers[j] = markers[j + 1];
         markers[j + 1] = tempExchangVal;
        }
        }
        i--;
      }
      return markers;
     }

    }

    function addMarkers(map, markerMap) {
    	map.addOverlay(markerMap.startMarker);
    	map.addOverlay(markerMap.endMarker);
    	markerMap.problemMarkers.forEach((marker) => {
    	  map.addOverlay(marker);
    	});
    }

    function addMarkerEventHandlers(map, markerMap) {
    	let pointTime = {
    	  startTime: '2016-9-13 13:56',
    	  inTime: '2016-9-13 14:56',
    	  endTime: '2016-9-13 15:56',
    	}
    	let startMarker = markerMap.startMarker,
    			endMarker = markerMap.endMarker,
    			problemMarkers = markerMap.problemMarkers;

    	// 图标点击事件-开始位置
    	let optsStart = {
    	  title: "<h3>开始位置</h3>", // 信息窗口标题
    	}

    	// 图标点击事件-结束位置
    	let optsEnd = {
    	  title: "<h3>结束位置</h3>", // 信息窗口标题
    	}

    	let infoWindowStart = new BMap.InfoWindow(startMarker.time, optsStart),
    	    infoWindowEnd = new BMap.InfoWindow(endMarker.time, optsEnd); // 创建信息窗口对象
    	startMarker.addEventListener("click", function() {
    	  map.openInfoWindow(infoWindowStart, startMarker.point); //开启信息窗口
    	});
    	endMarker.addEventListener("click", function() {
    	  map.openInfoWindow(infoWindowEnd, endMarker.point); //开启信息窗口
    	});
    	problemMarkers.forEach((marker) => {
    	  marker.addEventListener("click", function() {
          let url = `${location.href}?problemId=${marker.problemId}`;
          let optsProblem = {
            title: `<h3>内容 ${marker.problemContent} <a ng-click="vm.showProblemById(${marker.problemId})" style="margin-left: 100px" href="javascript:;">详情</a></h3>`, // 信息窗口标题
          }
          let infoWindow = new BMap.InfoWindow(marker.time, optsProblem);
    	    map.openInfoWindow(infoWindow, marker.point); // 开启信息窗口
          $compile($('.track-content-outer').contents())($scope);  
    	  });
    	});         
    }

    function drawTrackLines(map, resPointData) {
    	let trackPois = resPoints.trackHistoryPoints;
    	map.addOverlay(new BMap.Polyline(trackPois, {strokeColor: '#111'}));
    	map.setViewport(trackPois);
    }

  }

}

export {specialDetailCtrl};